﻿﻿using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;

public class DeckDisplayer : UIPanel
{
    public BaseEntityData playerEntityData;
    public GameObject container;

    public GameObject markerPrefab;
    
    private Camera _camera;
    private Dictionary<int, CardPair> quickSlot = new Dictionary<int, CardPair>();
    
    private Vector3 mouseDownPosition = Vector3.zero;
    
    private void Awake()
    {
        if (_camera == null)
        {
            _camera = Camera.main;
        }
    }

    public void InitialDeckData(BaseEntityData data)
    {
        this.playerEntityData = data;

        InitialDeckCards();
    }

    public void InitialDeckCards()
    {
        
        foreach (CardPair pairData in playerEntityData.currentDeck)
        {
            // 正常初始化Basic卡牌，然后放到展示区里面
            GameObject cardEntity = CardUtils.GetCardInstance(pairData, null, pairData.belongSlot);
            
            cardEntity.transform.SetParent(container.transform);
            cardEntity.transform.localScale = Vector3.one;
            
            // 如果卡牌是已经绑到快捷栏里面了，那么就顺便设置在快捷里面
            FlushCard(cardEntity);
        }
    }

    public void FlushAllCard()
    {
        for (int i = 0; i < container.transform.childCount; i++)
        {
            GameObject cardObject = container.transform.GetChild(i).gameObject;
            
            // TODO 刷新显示的卡牌状态
            FlushCard(cardObject);
        }
    }

    private void FlushCard(GameObject cardObject)
    {
        cardObject.GetComponent<CardDisplayerMonobehaviour>().FlushCardDisplay();
        
        var pairData = cardObject.GetComponent<BasicCard>().pairData;
        
        if (pairData.cardDeckType == DeckType.Fixed)
        {
            cardObject.transform.Find("Marker").transform.localScale = Vector3.one;
            cardObject.transform.Find("Marker/Text").GetComponent<TextMeshProUGUI>().SetText((pairData.belongSlot + 1).ToString());

            quickSlot[pairData.belongSlot] = pairData;
        }
        else
        {
            cardObject.transform.Find("Marker").transform.localScale = Vector3.zero;
            cardObject.transform.Find("Marker/Text").GetComponent<TextMeshProUGUI>().SetText("");
        }
    }

    private void Update()
    {
        // TODO 检测Raycast点击
        // TODO 如果是panel，那么就让UIManager退出当前窗口
        // TODO 如果是卡牌，那么就让UIManager显示卡牌的详情

        if (isActive && Input.GetMouseButtonDown(0))
        {
            mouseDownPosition = Input.mousePosition;
        }
        
        if (isActive && Input.GetMouseButtonUp(0))
        {
            BasicCard basicCard = null;
            
            foreach (RaycastResult result in OnClickGenerate())
            {
                if (result.gameObject.name.Contains("Card"))
                {
                    basicCard = result.gameObject.GetComponent<BasicCard>();
                }
            }

            if (basicCard != null && mouseDownPosition == Input.mousePosition)
            {
                // 让UI管理器来显示详情
                UiManager.DisplayCardDetailPanel(basicCard.pairData, quickSlot);

            }
        }

        CloseWithRightClick();
    }
}
